<?php

	/**
	 * @package		CoreProject
	 * @subpackage	Mime
	 *
	 * cpr_Mime_HeaderParameter
	 * cpr_Mime_HeaderParameters
	 *
	 */

	class cpr_Mime_HeaderParameter
	{
		/**
		 * @var	string
		 */
		private $_attribute = '';

		/**
		 * @var	string
		 */
		private $_value = '';

		/**
		 * @var	cpr_Mime_GlobalCharset
		 */
		private $_global_charset;

		/**
		 * @param	cpr_Mime_GlobalCharset	&$_gc
		 * @param	string					$_paramAttribute = ''
		 * @param	string					$_paramValue = ''
		 * @return	cpr_Mime_HeaderParameter
		 */
		public function cpr_Mime_HeaderParameter(&$_gc, $_paramAttribute = '', $_paramValue = '')
		{
			$this->_global_charset =& $_gc;

			if ($_paramAttribute != '' || $_paramValue != '')
			{
				$this->_attribute = $_paramAttribute;
				$this->_value = $_paramValue;
			}
		}

		/**
		 * @param	string	$_param
		 * @param	string	$_separator = '='
		 */
		public function Parse($_param, $_separator = '=')
		{
			$parts = explode($_separator, $_param, 2);

			$this->_attribute = trim(trim($parts[0]), '"\'');
			if (count($parts) == 2)
			{
				$this->_value = trim(trim($parts[1]), '"\'');
			}
		}

		/**
		 * @return string
		 */
		public function ToEncodeString()
		{
			$value = $this->_value;

			if (!cpr_Core_Utils::IsLatin($value))
			{
				$value = cpr_Mime_Utils::EncodeHeaderString($value, $this->_global_charset->GetMailCharset(), $this->_global_charset->GetOutCharset(), true);
			}

			if ($this->_attribute != '' && $this->_value != '')
			{
				return sprintf('%s="%s"', $this->_attribute, $value);
			}
			elseif ($this->_attribute != '')
			{
				return $this->_attribute;
			}
			elseif ($this->_value != '')
			{
				return $value;
			}

			return '';
		}

		/**
		 * @return string
		 */
		public function ToString()
		{
			$value = $this->_value;

			if ($this->_attribute != '' && $this->_value != '')
			{
				return sprintf('%s="%s"', $this->_attribute, $value);
			}
			elseif ($this->_attribute != '')
			{
				return $this->_attribute;
			}
			elseif ($this->_value != '')
			{
				return $value;
			}

			return '';
		}

		/**
		 * @return	string
		 */
		public function Value()
		{
			return $this->_value;
		}

		/**
		 * @return	string
		 */
		public function Attribute()
		{
			return $this->_attribute;
		}
	}

	class cpr_Mime_HeaderParameters extends cpr_Core_Collection
	{
		/**
		 * @var	cpr_Mime_GlobalCharset
		 */
		private $_global_charset;

		/**
		 * @param	cpr_Mime_GlobalCharset	&$_gc
		 * @param	string					$params = null
		 * @return	cpr_Mime_HeaderParameters
		 */
		public function cpr_Mime_HeaderParameters(&$_gc, $params = null)
		{
			$this->_global_charset =& $_gc;

			if (null !== $params)
			{
				$this->Parse($params);
			}
		}

		/**
		 * @param	string	$dataToParse
		 */
		public function Parse($dataToParse)
		{
			$values = explode(';', $dataToParse);

			foreach ($values as $param)
			{
				$hparam = new cpr_Mime_HeaderParameter($this->_global_charset);
				$hparam->Parse(trim($param));
				$this->Add($hparam);
				unset($hparam);
			}
		}

		/**
		 * @return string
		 */
		public function ToEncodeString()
		{
			return $this->_toString(true);
		}

		/**
		 * @return string
		 */
		public function ToString()
		{
			return $this->_toString(false);
		}

		public function &GetByName($_name)
		{
			for ($i = 0, $c = $this->Count(); $i < $c; $i++)
			{
				$param =& $this->Get($i);
				if (strtolower($param->Attribute()) == strtolower($_name))
				{
					return $param;
				}
			}
			$null = null;
			return $null;
		}

		/**
		 * @return string
		 */
		private function _toString($isEncode)
		{
			$result = '';
			$newLine = ';'.CPR_CRLF.CPR_TAB;
			if ($this->Count() > 0)
			{
				$params =& $this->Instance();
				foreach ($params as $param)
				{
					$result .= ($isEncode ? $param->ToEncodeString() : $param->ToString()).$newLine;
				}

				$result = substr($result, 0, strlen($result) - strlen($newLine));
			}
			return $result;
		}
	}
